package codeRandomThoughts.Test203移除链表元素;

import codeRandomThoughts.utils.ListNode;

/**
 * Definition for singly-linked list.
 * public class ListNode {
 * int val;
 * ListNode next;
 * ListNode() {}
 * ListNode(int val) { this.val = val; }
 * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
//二刷
public class Solution2 {
    public ListNode removeElements(ListNode head, int val) {
        ListNode dummyHead = new ListNode();
        dummyHead.next = head;
        ListNode node = dummyHead;
        while (node.next != null) {
            if (node.next.val == val) {
                //删除节点
                node.next = node.next.next;
                //此时node节点不用前移,因为node.next已经更新
            } else {
                //本质是更新node.next
                node = node.next;
            }
        }
        return dummyHead.next;
    }
}

